export const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

/**
 * 创建获取标签名的函数
 * @param {any[]} arr 列表数据
 * @param {{ value: string, label: string }} config 字段配置
 * @returns {(v: any) => string} 标签名
 */
export const createGetLabelFunc = (arr, config = {}) => {
  const { value = 'value', label = 'label' } = config
  const _map = arr.reduce((t, _) => {
    const key = _[value]
    return { ...t, [key]: _[label] }
  }, {})
  return v => _map[v]
}

/** 问题类型选择 */
export const questionTypeSelect = (() => {
  const enums = {
    QA: 0,
    SELECT: 1,
    MULTIPLE_SELECT: 2,
  }

  const data = (T => ([
    { value: T.QA, label: '问答题' },
    { value: T.SELECT, label: '单选题' },
    { value: T.MULTIPLE_SELECT, label: '多选题' }
  ]))(enums)

  return { ...enums, data, getLabel: createGetLabelFunc(data) }
})()

export const getAnswerResult = v => {
  if (v === 0) {
    return { label: '错误', icon: '/images/error.png' }
  } else if (v === 1) {
    return { label: '正确', icon: '/images/success.png' }
  } else if (v && v >= 0.5 && v < 1) {
    return { label: '部分正确', icon: '/images/warning.png' }
  }
  return {}
}

export const isEmpty = v => ['', undefined, null].includes(v)

export const isEmptyForTrim = v => isEmpty(v) || !v.trim()

export const isArr = v => v instanceof Array
